package cn.tedu.charging.user.dao.repository.impl;

import cn.tedu.charging.common.enums.VehicleUserBindEnum;
import cn.tedu.charging.common.pojo.po.ChargingUserVehicleBindPO;

import cn.tedu.charging.user.dao.mapper.ChargingUserVehicleBindMapper;
import cn.tedu.charging.user.dao.repository.UserVehicleRepository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserVehicleRepositoryImpl implements UserVehicleRepository {
    @Autowired
    private ChargingUserVehicleBindMapper chargingUserVehicleBindMapper;


    @Override
    public Integer getUserBindedVehicleId(Integer userId, Integer state) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state",state);
        ChargingUserVehicleBindPO po = chargingUserVehicleBindMapper.selectOne(queryWrapper);
        if (po!=null){
            return po.getVehicleId();
        }else {
            return null;
        }
    }

    @Override
    public void save(ChargingUserVehicleBindPO po) {
        chargingUserVehicleBindMapper.insert(po);
    }

    @Override
    public Boolean updateUserVehicleBindState(Integer userId) {
        UpdateWrapper<ChargingUserVehicleBindPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",userId);
        updateWrapper.set("state", VehicleUserBindEnum.BINDED.getState());
        return chargingUserVehicleBindMapper.update(null,updateWrapper)>0;
    }

}
